﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using coffeeshop.coffeeDataSetTableAdapters;

namespace coffeeshop.Models
{
    public class ProductCategory
    {
        private static ProductCategoryTableAdapter _adapter = new ProductCategoryTableAdapter();

        public string Name { get; set; }
        public int ProductCategoryId { get; set; }
        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }

        public ProductCategory() { }
        public ProductCategory(int productCateId) {
            ProductCategoryId = productCateId;
        }

        public static coffeeDataSet.ProductCategoryDataTable LoadCategories(bool toHideInactive)
        {
            return _adapter.GetProductCategoryList(toHideInactive);
        }

        public bool UpdateActiveness()
        {
            try
            {
                int result = _adapter.UpdateActiveness(IsActive, ProductCategoryId);

                return result == 1;
            }
            catch
            {
                return false;
            }
        }

        public bool Save()
        {
            try
            {
                if (ProductCategoryId == 0)
                {
                    coffeeDataSet.ProductCategoryDataTable data = _adapter.AddProductCate(Name);
                    ProductCategoryId = (int)data.Rows[0]["ProductCategoryId"];
                    Name = (string)data.Rows[0]["Name"];
                    IsActive = (bool)(data.Rows[0]["IsActive"]);
                    CreatedDate = (DateTime)data.Rows[0]["CreatedDate"];
                }
                else
                {
                    _adapter.UpdateProductCate(Name, ProductCategoryId);
                }

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
